package com.yuexun.dhdoctor.module.contractmanage.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseEntity;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.contractmanage.adapter.HospitalAdviceAdapter;
import com.yuexun.dhdoctor.module.contractmanage.adapter.RecordOutpatientInspectAdapter;
import com.yuexun.dhdoctor.module.contractmanage.adapter.RecordOutpatientTestAdapter;
import com.yuexun.dhdoctor.module.contractmanage.bean.HospitalizationAdviceBean;
import com.yuexun.dhdoctor.module.contractmanage.bean.HospitalizationRecordBean;
import com.yuexun.dhdoctor.module.contractmanage.bean.InspectBean;
import com.yuexun.dhdoctor.module.contractmanage.bean.TestBean;
import com.yuexun.dhdoctor.module.contractmanage.model.impl.ContractManageModel;
import com.yuexun.dhdoctor.module.contractmanage.presenter.impl.HospitalizationAdvicePresenter;
import com.yuexun.dhdoctor.module.contractmanage.presenter.impl.InspectPresenter;
import com.yuexun.dhdoctor.module.contractmanage.presenter.impl.TestPresenter;
import com.yuexun.dhdoctor.module.contractmanage.ui.view.IHospitalizationAdviceView;
import com.yuexun.dhdoctor.module.contractmanage.ui.view.IInspectView;
import com.yuexun.dhdoctor.module.contractmanage.ui.view.ITestView;
import com.yuexun.dhdoctor.widget.FixedSwipeListView;

import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2016/4/13.
 */
public class Act_RecordHospital extends BaseKJActivity implements IInspectView,ITestView,IHospitalizationAdviceView {
    @Bind(R.id.title_tv)
    TextView titleTv;
    @Bind(R.id.recordhospital_date)
    TextView recordhospitalDate;
    @Bind(R.id.recordhospital_visitno)
    TextView recordhospitalVisitno;
    @Bind(R.id.recordhospital_hospital_name)
    TextView recordhospitalHospitalName;
    @Bind(R.id.recordhospital_type)
    TextView recordhospitalType;
    @Bind(R.id.recordhospital_diagnosis)
    TextView recordhospitalDiagnosis;
    @Bind(R.id.recordhospital_doctor_name)
    TextView recordhospitalDoctorName;
    @Bind(R.id.recordhospital_department)
    TextView recordhospitalDepartment;
    @Bind(R.id.recordhospital_icd)
    TextView recordhospitalIcd;
    @Bind(R.id.recordhospital_disease)
    TextView recordhospitalDisease;
    @Bind(R.id.record_inspect_list)
    FixedSwipeListView recordInspectList;
    @Bind(R.id.record_test_list)
    FixedSwipeListView recordTestList;
    @Bind(R.id.record_advice_list)
    FixedSwipeListView recordAdviceList;

    private BaseAdapter adapter_test;
    private BaseAdapter adapter_inspect;
    private BaseAdapter adapter_advice;

    private InspectPresenter presenter_inspect;
    private TestPresenter presenter_test;
    private HospitalizationAdvicePresenter presenter;

    List<InspectBean.RowsBean> checkDataList = new ArrayList<>();
    List<TestBean.RowsBean> chemicalDataList = new ArrayList<>();
    List<HospitalizationAdviceBean.RowsBean> adviceDataList = new ArrayList<>();
    String name;

    private HospitalizationRecordBean.RowsBean hospitalizationRecordBean = new HospitalizationRecordBean.RowsBean();

    @Override
    public void setRootView() {
        setContentView(R.layout.act_recordhospital);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        setTitle();
        hospitalizationRecordBean = (HospitalizationRecordBean.RowsBean)getIntent().getExtras().get("HospitalizationRecordBean");
        name = getIntent().getExtras().getString("name");
        setContent();
        presenter = new HospitalizationAdvicePresenter(this,new ContractManageModel());
        presenter_inspect = new InspectPresenter(this,new ContractManageModel());
        presenter_test = new TestPresenter(this,new ContractManageModel());

        recordInspectList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString("hosId", checkDataList.get(position).getHosp_id());
                bundle.putString("applyNo", checkDataList.get(position).getApply_no());
                Intent intent = new Intent(aty, Act_ContractManageInspectDetail.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        recordTestList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString("hosId", chemicalDataList.get(position).getHosp_id());
                bundle.putString("rpeportNo", chemicalDataList.get(position).getReport_no());
                Intent intent = new Intent(aty, Act_ContractManageTestDetail.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        ApiForGetList_Inspect();
        ApiForGetList_Test();
        ApiForGetList_Advice();
    }

    private void ApiForGetList_Inspect() {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("pageIndex", 1+"");
        params.put("pageSize", 100+"");
        params.put("sourceNo", hospitalizationRecordBean.getInp_no());
        params.put("hospId", hospitalizationRecordBean.getHosp_id());
        params.put("sourceId", "");
        presenter_inspect.showInspectDate(params);
    }

    private void ApiForGetList_Test() {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("pageIndex", 1+"");
        params.put("pageSize", 100+"");
//        params.put("personId", hospitalizationRecordBean.getPerson_id());
        params.put("hospId", hospitalizationRecordBean.getHosp_id());
        params.put("sourceNo", hospitalizationRecordBean.getInp_no());
        presenter_test.showTestDate(params);
    }

    private void ApiForGetList_Advice() {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("pageIndex", 1+"");
        params.put("pageSize", 100+"");
        params.put("hospId", hospitalizationRecordBean.getHosp_id());
        params.put("inpNo", hospitalizationRecordBean.getInp_no());
        presenter.showHospitalizationAdviceDate(params);
    }

    private void setContent()
    {
        recordhospitalDate.setText("就诊日期  "+hospitalizationRecordBean.getInhos_dtime());
        recordhospitalVisitno.setText("住院号  "+hospitalizationRecordBean.getInp_id());
        recordhospitalHospitalName.setText(hospitalizationRecordBean.getHosp_name());
        recordhospitalType.setText("住院");
        recordhospitalDiagnosis.setText(hospitalizationRecordBean.getInhos_diag_name());
        recordhospitalDoctorName.setText(hospitalizationRecordBean.getDiag_doct_name());
        recordhospitalDepartment.setText(hospitalizationRecordBean.getDiag_dept_name());
    }

    protected void setTitle() {
        titleTv.setText("住院记录");
    }

    @Override
    @OnClick({R.id.back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }

    @Override
    public void showTestDate(BaseEntity<TestBean> list) {
        chemicalDataList = list.getDatas().get(0).getRows();
        adapter_test = new RecordOutpatientTestAdapter(recordTestList, chemicalDataList, R.layout.recordhospital_item);
        recordTestList.setAdapter(adapter_test);
    }

    @Override
    public void showInspectDate(BaseEntity<InspectBean> list) {
        checkDataList = list.getDatas().get(0).getRows();
        adapter_inspect = new RecordOutpatientInspectAdapter(recordInspectList, checkDataList, R.layout.recordhospital_item);
        recordInspectList.setAdapter(adapter_inspect);
    }

    @Override
    public void showHospitalizationAdviceDate(BaseEntity<HospitalizationAdviceBean> list) {
        adviceDataList = list.getDatas().get(0).getRows();
        adapter_advice = new HospitalAdviceAdapter(recordAdviceList, adviceDataList, R.layout.item_outpatient_advice);
        recordAdviceList.setAdapter(adapter_advice);
    }
}
